package com.tools;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import javax.management.DynamicMBean;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAction;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.StyledEditorKit.ForegroundAction;

import com.mysqld.Mysqld;

public class Tools {

	
	//设置窗口居中
	public static void setWindowPos(int WIDTH,int HEIGHT,JFrame jframe) {
		//传递过来宽和高
		   //设置成静态可以随时随地调用
        //设置窗口的位置以及大小  将窗口传递过来
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
        int width=screenSize.width;
        int height=screenSize.height;//取宽度和高度
        int x=(width-WIDTH)/2;
        int y=(height-HEIGHT)/2;
        jframe.setBounds(x, y, WIDTH, HEIGHT);
	}
	//返回当前项目同级别目录
	public static String  getPath() {
	

		String separator = File.separator;
		String tempString="";
		String path = System.getProperty("user.dir");
		int i=0;
		for (String str : Arrays.asList(path.split("\\" + separator))) {
			i++;
			
		}
		
		int j=0;
		for (String str : Arrays.asList(path.split("\\" + separator))) {
			
		
			j++;
			tempString=tempString+str+"\\";
			
		}
	
		return tempString;
	}
//弹窗
	public static void messageWindows(String msg) {
		JOptionPane.showMessageDialog(null,  msg,"消息",JOptionPane.WARNING_MESSAGE);
	}
	//获取密码
	public static String getPassword(JPasswordField jp) {
		String password=new String(jp.getPassword());
		return password;
	}
	//向表格当中添加数据
		public static  int addDataTable(ResultSet rs ,DefaultTableModel  model,int index) {
			int count=0;
			model.setNumRows(0);
			String  data[]=new String [index];
			try {
				while(rs.next()) {
					count++;
					for(int i=0;i<data.length;i++) {
						data[i]=rs.getString(i+1);
						
					}
					model.addRow(data);
					
				}
				rs.close();
				return count;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return count;
			}
		}
		//设置表格大小
		public static void setTableSize(JTable table,int WIDTH,int num) {
			
			int h=0;
			for(int i=1;i<=num;i++) {
				h=h+16;
			}
			table.setPreferredSize(new Dimension(WIDTH-30,h));//设置表格在盘子当中的大下
		}
		public static void upDateComboBox(String strs[],JComboBox jComboBox,String str) {//数据库语句 第二个是要更新的下拉框
			
			
			jComboBox.removeAllItems();
			jComboBox.addItem("--"+str+"--");
			
			for(int i=0;i<strs.length;i++) {
				jComboBox.addItem(strs[i]);
				
			}
					
			
		
			
		}
		public static void upDateComboBox(String strsql,JComboBox jComboBox,String str,String prams) {//数据库语句 第二个是要更新的下拉框
			
			ResultSet rs = Mysqld.QueryData(strsql, null);
			jComboBox.removeAllItems();
			jComboBox.addItem("--"+str+"--");
			try {
				int j=0;
				while(rs.next()) {
					String name=rs.getString(prams);
					jComboBox.addItem(name);//在尾部追加
					}
				rs.first();
				if(rs==null) {
					jComboBox.addItem("--"+str+"--");
				}
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
}
